package cn.yuanqiao.archive.danganzhengli.service.impl;

import cn.yuanqiao.archive.appManager.domain.TenantCategory;
import cn.yuanqiao.archive.appManager.domain.TenantMdataList;
import cn.yuanqiao.archive.appManager.mapper.TenantCategoryMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantMdataListMapper;
import cn.yuanqiao.archive.danganzhengli.domain.HandOverDa;
import cn.yuanqiao.archive.danganzhengli.domain.vo.HandOverDaVo;
import cn.yuanqiao.archive.danganzhengli.mapper.HandOverDaMapper;
import cn.yuanqiao.archive.danganzhengli.service.IHandOverDaService;
import cn.yuanqiao.common.core.domain.entity.SysDept;
import cn.yuanqiao.common.core.domain.entity.SysMenu;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.manage.domain.ManageFonds;
import cn.yuanqiao.manage.mapper.ManageFondsMapper;
import cn.yuanqiao.system.mapper.SysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static cn.yuanqiao.common.utils.PageUtils.startPage;

/**
 * @author zjj
 * @date 2023/7/21
 */
@Service
public class HandOverDaServiceImpl implements IHandOverDaService {
    @Autowired
    private HandOverDaMapper handOverDaMapper;
    @Autowired
    private TenantCategoryMapper categoryMapper;
    @Autowired
    private ManageFondsMapper manageFondsMapper;
    @Autowired
    private TenantMdataListMapper tenantMdataListMapper;
    @Autowired
    private SysMenuMapper menuMapper;

    @Value("${YJ_URL}")
    private String yjUrl;

    @Override
    @Transactional
    public String insertHandOverDa(Long categoryId,Long [] ids) {
        StringBuilder msg = new StringBuilder();
        Long userId = SecurityUtils.getUserId();
        TenantCategory category = categoryMapper.selectTenantCategoryByID(categoryId,new TenantCategory().getTableName());
        Boolean bl = false;
        for(Long id:ids){
            HandOverDa handOverDa = new HandOverDa();
            handOverDa.setDaId(id);
            HandOverDa data = handOverDaMapper.selectHandOverDa(handOverDa);
            if(data == null){
                handOverDa.setCategoryId(categoryId);
                handOverDa.setCreateId(userId);
                handOverDa.setDaType(category.getType());
                handOverDa.setState(0);
                handOverDaMapper.insertHandOverDa(handOverDa);
            }else {
                bl = true;
            }
        }
        if(bl){
            msg.append("部分数据存在移交单中或者已移交完成，已跳过");
        }
        return msg.toString();
    }

    @Override
    @Transactional
    public String delHandOverDa(Long[] ids) {
        StringBuilder msg = new StringBuilder();
        Boolean bl = false;
        for(Long id:ids){
            HandOverDa handOverDa = new HandOverDa();
            handOverDa.setId(id);
            HandOverDa data = handOverDaMapper.selectHandOverDa(handOverDa);
            if(data!=null && data.getState() == 0){
                handOverDaMapper.deleteHandOverDaById(handOverDa);
            }else {
                bl = true;
            }
        }
        if(bl){
            msg.append("部分数据在移交中或者移交完成，无法删除，已跳过该数据");
        }
        return msg.toString();
    }

    @Override
    public List<HandOverDaVo> selectHandOverDaVo(HandOverDa handOverDa) {
        String tenantCode = SecurityUtils.getTenantCode();
        TenantCategory category = categoryMapper.selectTenantCategoryByID(handOverDa.getCategoryId(),new TenantCategory().getTableName());
        List<HandOverDaVo> list = new ArrayList<>();
        if(category.getType() == 3){
            TenantMdataList select = new TenantMdataList();
            select.setType(category.getType());
            select.setGroupId(category.getGroupId());
            select.setFieldName("MJ");
            List<TenantMdataList> lists = tenantMdataListMapper.selectTenantMdataListListPrecise(select);
            //判断是否有密级字段
            String mj = "''";
            if(lists.size()>0){
                mj = "a.MJ";
            }
            select.setFieldName("BGQX");
            lists = tenantMdataListMapper.selectTenantMdataListListPrecise(select);
            //判断是否有密级字段
            String bgqx = "''";
            if(lists.size()>0){
                bgqx = "a.BGQX";
            }
            startPage();
            list = handOverDaMapper.selectHandOverDaVoByItem(handOverDa,category.getItemTableName(),category.getFolderTableName(),category.getFileTableName(),mj,bgqx,tenantCode);
        }else if(category.getType() == 1){
            startPage();
            list = handOverDaMapper.selectHandOverDaVoByFolder(handOverDa,category.getFolderTableName(),category.getFileTableName(),tenantCode);
        }else if(category.getType() == 0){
            startPage();
            list = handOverDaMapper.selectHandOverDaVoByFile(handOverDa,category.getFileTableName(),tenantCode);
        }
        return list;
    }

    @Override
    public List<HandOverDaVo> selectHandOverDa(HandOverDa handOverDa) {
        String tenantCode = SecurityUtils.getTenantCode();
        TenantCategory category = categoryMapper.selectTenantCategoryByID(handOverDa.getCategoryId(),new TenantCategory().getTableName());
        List<HandOverDaVo> list = new ArrayList<>();
        if(category.getType() == 3){
           // startPage();
            TenantMdataList select = new TenantMdataList();
            select.setType(category.getType());
            select.setGroupId(category.getGroupId());
            select.setFieldName("MJ");
            List<TenantMdataList> lists = tenantMdataListMapper.selectTenantMdataListListPrecise(select);
            //判断是否有密级字段
            String mj = "''";
            if(lists.size()>0){
                mj = "a.MJ";
            }
            select.setFieldName("BGQX");
            lists = tenantMdataListMapper.selectTenantMdataListListPrecise(select);
            String bgqx = "''";
            if(lists.size()>0){
                bgqx = "a.BGQX";
            }
            list = handOverDaMapper.selectHandOverDaVoByItem(handOverDa,category.getItemTableName(),category.getFolderTableName(),category.getFileTableName(),mj,bgqx,tenantCode);
        }else if(category.getType() == 1){
          //  startPage();
            list = handOverDaMapper.selectHandOverDaVoByFolder(handOverDa,category.getFolderTableName(),category.getFileTableName(),tenantCode);
        }else if(category.getType() == 0){
          //  startPage();
            list = handOverDaMapper.selectHandOverDaVoByFile(handOverDa,category.getFileTableName(),tenantCode);
        }
        return list;
    }

    @Override
    public SysDept selectDept(String categoryId) {
        String tenantCode = SecurityUtils.getTenantCode();
        TenantCategory category = categoryMapper.selectTenantCategoryByID(Long.parseLong(categoryId),new TenantCategory().getTableName());
        ManageFonds manageFonds= manageFondsMapper.selectManageFondsByCode(category.getFondsCode(),tenantCode);
        if(manageFonds.getDeptId()!=null){
            SysDept sysDept=handOverDaMapper.selectDeptById(manageFonds.getDeptId(),tenantCode);
            return sysDept;
        }else{
            throw new ServiceException("该全宗没有立档单位！");
        }
    }

    @Override
    public boolean getAuthority() {
        //获取该用户是否有在线记录的权限
        //archive/archiveTransfer/online/index
        boolean flag=false;
        List<SysMenu> menus = menuMapper.selectMenuTreeByUserIdUrl(SecurityUtils.getUserId(),SecurityUtils.getTenantCode(),yjUrl);
        if(menus.size()>0){
            flag=true;
        }
        return flag;
    }
}
